//-----------------------------------------------------------------------------
// F3xx_USB0_ReportHandler.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// Contains functions and variables dealing with Input and Output
// HID reports.
// How To Test:    See Readme.txt
//
//
// FID:            
// Target:         C8051F3xx
// Tool chain:     Keil / Raisonance
//                 Silicon Laboratories IDE version 2.6
// Command Line:   See Readme.txt
// Project Name:   F3xx_FirmwareTemplate
//
// Release 1.1
//    - Minor changes to F3xx_USB0_Descriptor.c
//    - 16 NOV 2006
// Release 1.0
//    -Initial Revision (PD)
//    -07 DEC 2005
//


// ----------------------------------------------------------------------------
// Header files
// ----------------------------------------------------------------------------

#include "F3xx_USB0_ReportHandler.h"
#include "F3xx_USB0_InterruptServiceRoutine.h"
#include "F3xx_USB0_CustomApp.h"
#include "flash_memory.h"

// ----------------------------------------------------------------------------
// Local Function Prototypes
// ----------------------------------------------------------------------------

// ****************************************************************************
// Add custom Report Handler Prototypes Here
// ****************************************************************************
void stub (void)
{

}
// ----------------------------------------------------------------------------
// Local Definitions
// ----------------------------------------------------------------------------

// ****************************************************************************
// Set Definitions to sizes corresponding to the number of reports
// ****************************************************************************

#define IN_VECTORTABLESize 1
#define OUT_VECTORTABLESize 1

// ----------------------------------------------------------------------------
// Global Constant Declaration
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Global Variable Declaration
// ----------------------------------------------------------------------------

BufferStructure IN_BUFFER, OUT_BUFFER;

// ----------------------------------------------------------------------------
// Local Variable Declaration
// ----------------------------------------------------------------------------

Flash_Address addr;

bit firstPacket = 1;

int bytesRemaining;
// ----------------------------------------------------------------------------
// Local Functions
// ----------------------------------------------------------------------------

void Send_String()
{
	
}

void Send_Next_String_Packet()
{

}
void Write_String_To_Flash(void)
{
	if (firstPacket)
	{
		addr.high = addr.medium = addr.low = 0;
		// Turn 2 8 bit values into a 16 bit value
		bytesRemaining = 0;
		bytesRemaining += OUT_BUFFER.Ptr[0];
		bytesRemaining <<= 8;
		bytesRemaining += OUT_BUFFER.Ptr[1];
	
		flash_erase_sector(addr);	// Erase the block for writing
		flash_write_bytes(addr,OUT_BUFFER.Ptr+1,50);	// FIX MAGIC CONSTANT
		addr.low += 50;

		bytesRemaining -= 48;
	}
	else
	{
		flash_write_bytes(addr,OUT_BUFFER.Ptr+3,48);	// FIX MAGIC CONSTANT
		if (addr.low > 207)		// 255-48 = 207
		{
			if (addr.medium == 255)
			{
				addr.high++;
				if (addr.high > 0x03) addr.high = 0;
			}
			addr.medium++;
		}
		addr.low += 48;
		bytesRemaining -=48;
		if (bytesRemaining <= 0)
		{
			firstPacket = 1;
		}
	}
}

// ****************************************************************************
// Add all Report Handler routines here
// ****************************************************************************


// ****************************************************************************
// For Input Reports:
// Point IN_BUFFER.Ptr to the buffer containing the report
// Set IN_BUFFER.Length to the number of bytes that will be
// transmitted.
//
// REMINDER:
// Systems using more than one report must define Report IDs
// for each report.  These Report IDs must be included as the first
// byte of an IN report.
// Systems with Report IDs should set the FIRST byte of their buffer
// to the value for the Report ID
// AND
// must transmit Report Size + 1 to include the full report PLUS
// the Report ID.
//
// ****************************************************************************



// ****************************************************************************
// For Output Reports:
// Data contained in the buffer OUT_BUFFER.Ptr will not be
// preserved after the Report Handler exits.
// Any data that needs to be preserved should be copyed from
// the OUT_BUFFER.Ptr and into other user-defined memory.
//
// ****************************************************************************


// ----------------------------------------------------------------------------
// Global Functions
// ----------------------------------------------------------------------------

// ****************************************************************************
// Configure Setup_OUT_BUFFER
//
// Reminder:
// This function should set OUT_BUFFER.Ptr so that it
// points to an array in data space big enough to store
// any output report.
// It should also set OUT_BUFFER.Length to the size of
// this buffer.
//
// ****************************************************************************
char xdata OUT_PACKET[64];
char xdata IN_PACKET[64];


void Setup_IN_BUFFER(void)
{
	IN_BUFFER.Ptr = IN_PACKET;
	IN_BUFFER.Length = 63;
}

void Setup_OUT_BUFFER(void)
{
	OUT_BUFFER.Ptr = OUT_PACKET;
	OUT_BUFFER.Length = 63;
}

// ----------------------------------------------------------------------------
// Vector Routines
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// ReportHandler_IN...
// ----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - Report ID
//
// These functions match the Report ID passed as a parameter
// to an Input Report Handler.
// the ...FG function is called in the SendPacket foreground routine,
// while the ...ISR function is called inside the USB ISR.  A lock
// is set whenever one function is called to prevent a call from the
// other from disrupting the routine.
// However, this should never occur, as interrupts are disabled by SendPacket
// before USB operation begins.
// ----------------------------------------------------------------------------
void ReportHandler_IN_ISR(unsigned char R_ID)
{
	Setup_IN_BUFFER();
	switch(R_ID)
	{
		case 1:
		Send_Next_String_Packet();

		
		default:
		break;
	}
}
void ReportHandler_IN_Foreground(unsigned char R_ID)
{
	Setup_IN_BUFFER();
	switch(R_ID)
	{
		case 1:		// Send our packet
		Send_Next_String_Packet();
		
		default:
		break;
	}
}



//Custom In_Handler functions
void StringLengthReportHandler(void)
{
	IN_PACKET[0] = 0;
}

void StringDataReportHandler(void)
{
	IN_PACKET[0] = 1;
}
// ----------------------------------------------------------------------------
// ReportHandler_OUT
// ----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - None
//
// This function matches the Report ID passed as a parameter
// to an Output Report Handler.
//
// ----------------------------------------------------------------------------

void ReportHandler_OUT(unsigned char R_ID)
{
	switch(R_ID)
	{
		case 2:		// Write the string to flash memory
		Write_String_To_Flash();
		break;

		case 3:		// Trigger returning the string
		Send_String();		

		default:
		break;
	}
}